<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8">
  <title>layuiAdmin 控制台主页一</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/lib/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/lib/layuiadmin/css/admin.css" media="all">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">账号信息</div>
                <div class="layui-card-body">
                    <div>
                        <label style="font-weight: bold;margin-right: 50px">appID</label>
                        <span th:text="${session.sysUser.appid}">mt4adf5c42d24ee7b1</span>
                    </div>
                    <hr class="layui-bg-gray">
                    <div>
                        <label style="font-weight: bold;margin-right: 22px">appsecret</label>
                        <span th:text="${session.sysUser.appsecret}">ce2fcdde4adf5c42d24ee7b1ac49ce82</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">安全设置</div>
                <div class="layui-card-body">
                    <div style="float: left">
                        <label style="font-weight: bold;margin-right: 50px">加签方式</label>
                        <span style="margin-right: 50px;">RSA(SHA256)密钥</span>
                    </div>
                    <div th:if="${#strings.isEmpty(session.sysUser.publicKey)}" id="setrsa_div">
                        <a style="text-decoration: none;cursor: pointer;color: #01ABED;" lay-submit lay-filter="setRsa">设置</a>
                    </div>
                    <div th:if="${!#strings.isEmpty(session.sysUser.publicKey)}">
                        <a th:data-id="${session.sysUser.id}" style="text-decoration: none;cursor: pointer;color: #01ABED;margin-right: 50px;" lay-submit lay-filter="checkUserRsa">查看应用公钥</a>
                        <a th:data-id="${session.sysUser.id}" style="text-decoration: none;cursor: pointer;color: #01ABED;" lay-submit lay-filter="checkPlatformRsa">查看平台公钥</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="_SETRSA_DIV">
    <a th:data-id="${session.sysUser.id}" style="text-decoration: none;cursor: pointer;color: #01ABED;margin-right: 50px;" lay-submit lay-filter="checkUserRsa">查看应用公钥</a>
    <a th:data-id="${session.sysUser.id}" style="text-decoration: none;cursor: pointer;color: #01ABED;" lay-submit lay-filter="checkPlatformRsa">查看平台公钥</a>
</script>
<!-- 新增 -->
<script type="text/html" id="rsaInput">
    <div class="layui-card">
        <div class="layui-card-body" style="font-size: 10px">
            <div>
                <div style="position: absolute;top: 10px;left: 7px;width: 10px;">
                    <i class="layui-icon layui-icon-about" style="font-size: 10px; color: #01ABED" />
                </div>
                <div style="margin-left: 10px">
                    使用SHA256withRsa，平台会用SHA256withRsa算法进行接口调用时的验签（不限制密钥长度）。使用
                    <a style="text-decoration: none;cursor: pointer;color: #01ABED;" lay-submit lay-filter="createRsa">平台密钥生成器</a>
                    或OpenSSL(第三方工具)生成密钥
                </div>
            </div>
            <form class="layui-form layui-form-pane">
                <textarea name="publicKey" required lay-verify="rsalen" placeholder="请输入" class="layui-textarea" style="height: 150px" th:text="${session.sysUser.publicKey}" />
                <div class="hidden-attribute">
                    <input name="id" th:value="${session.sysUser.id}">
                    <button class="layui-btn submit" lay-submit="" lay-filter="add">新增</button>
                    <button class="layui-btn reset" type="reset">重置</button>
                </div>
            </form>
        </div>
    </div>
</script>
<!--查看-应用公钥-->
<script type="text/html" id="checkUserRsa">
<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-elem-field" style="border: 1px solid #009688; border-radius: 5px;">
            <div style="font-size: 10px; background-color: #F0FFFF; border-radius: 5px;">
                <div style="position: absolute;top: 35px;left: 22px;width: 10px;">
                    <i class="layui-icon layui-icon-about" style="font-size: 20px; color: #009688" />
                </div>
                <div style="margin-left: 30px">
                    应用调用接口时的加签格式同时支持公钥和公钥证书，开发者可根据自己的实际情况选择使用，如果同时设置了应用公钥和公钥证书，则两种方式的公钥都可生效使用SHA256withRsa，平台会用SHA256withRsa算法进行接口调用时的密钥解密（不限制密钥长度）
                </div>
            </div>
        </div>
        <blockquote class="layui-elem-quote" style="padding: 0px; background-color: white;">
            <span style="padding-left: 10px; font-size: 18px;">
                应用公钥
            </span>
            <span   style="float: right">
                <a style="text-decoration: none;cursor: pointer;color: #01ABED;" lay-submit lay-filter="updateRsa">修改</a>
            </span>
        </blockquote>
        <div style="word-wrap: break-word; font-size: 10px;padding-left: 16px;">
            {{d.publicKey}}
        </div>
    </div>
</div>
</script>
<!--查看-平台公钥-->
<script type="text/html" id="checkPlatformRsa">
    <div class="layui-card">
        <div class="layui-card-body">
            <blockquote class="layui-elem-quote" style="padding: 0px; background-color: white;">
                <span style="padding-left: 10px; font-size: 18px;">
                    平台公钥
                </span>
            </blockquote>
            <div style="word-wrap: break-word; font-size: 10px;padding-left: 16px;">
                {{d.platformPublicKey}}
            </div>
        </div>
    </div>
</script>
<script src="/lib/layuiadmin/layui/layui.js"></script>
<script>
layui.config({
    base: '/lib/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index'], function() {
    var form = layui.form, laytpl = layui.laytpl, $ = layui.jquery, layerIndex = 1,
        $setrsa_div = $('#setrsa_div');

    // 新增用户公钥
    form.on('submit(setRsa)', function(obj) {
        laytpl(rsaInput.innerHTML).render(obj, function(html) {
            popupWin('应用公钥(SHA256withRsa)', html, true);
        });
        return false;
    });
    form.on('submit(add)', function(data) {
        operate('POST', data.field, false);
        return false;
    });

    // 生成Rsa
    form.on('submit(createRsa)', function() {
        window.open('/system/console/creatersa');
    });
    // 修改rsa
    form.on('submit(updateRsa)', function(obj) {
        layer.close(layerIndex);
        laytpl(rsaInput.innerHTML).render(obj, function(html) {
            console.log(html);
            popupWin('应用公钥(SHA256withRsa)', html, true);
        });
    })

    // 查看应用公钥
    form.on('submit(checkUserRsa)', function(obj) {
        $.get("/system/console/" + $(obj.elem).data('id'), function(res) {
            laytpl(checkUserRsa.innerHTML).render(res.data, function(html) {
                popupWin('应用公钥(SHA256withRsa)', html, false);
            })
        })
    });
    // 查看平台公钥
    form.on('submit(checkPlatformRsa)', function(obj) {
        $.get("/system/console/" + $(obj.elem).data('id'), function(res) {
            laytpl(checkPlatformRsa.innerHTML).render(res.data, function(html) {
                popupWin('平台公钥(SHA256withRsa)', html, false);
            })
        })
    });

    /**
     * 用户操作
     * 删除操作时传入的当前行对象
     * @param ajaxType 请求类型
     * @param data 参数
     * @param refresh 是否刷新
     */
    function operate(ajaxType, data) {
        var url = '/system/console/';
        $.ajax({
            url: url,
            type: ajaxType,
            async: true,
            data: data,
            success: function(res) {
                if(res.code == 10000) {
                    layer.close(layerIndex);
                    if(ajaxType === 'POST') {
                        laytpl(_SETRSA_DIV.innerHTML).render(res, function(html) {
                            $setrsa_div.html(html);
                        })
                    }
                    layer.msg(res.message, {icon: 1});
                }  else if(res.code == 403) {
                    layer.msg(res.message, {icon: 4});
                } else {
                    layer.msg(res.message, {icon: 5});
                }
            }
        })
    }

    /**
     * 渲染弹框
     * @param title 标题
     * @param content 内容
     * @param showBtn   是否显示按钮
     */
    function popupWin(title, content, showBtn) {
        layerIndex = layer.open({
            // id: 'dyrj',
            type: 1, //Page层类型
            area: '700px', //['700px', '480'],
            maxWidth: 1000,
            maxHeight: 800,
            skin: 'layui-layer-rim', //加上边框
            title: title,
            shade: 0.1, //遮罩透明度
            // maxmin: true, //允许全屏最小化
            anim: 0, //0-6的动画形式，-1不开启
            scrollbar: false, //禁用滚动
            content: content,
            btn: showBtn ? ['<i class="layui-icon layui-icon-ok"></i> 提交', '<i class="layui-icon layui-icon-refresh-1"></i> 重置'] : '',
            yes: function(index, layero) {
                layero.find('form .submit').trigger('click');
                return false;
            },
            btn2: function(index, layero) {
                layero.find('form .reset').trigger('click');
                return false;
            }
        });
        // layer.full(layerIndex);
    }

    form.verify({
        rsalen: [
            /^[\S]{200,4000}$/,
            '请填写200至4000个字符'
        ]
    });
});
</script>
</body>
</html>

